package com.cloud.xtools.user.controller.admin;

import com.cloud.xtools.common.controller.BaseController;
import com.cloud.xtools.common.response.ApiResponse;
import com.cloud.xtools.common.response.PageList;
import com.cloud.xtools.common.validator.annotation.EntityExist;
import com.cloud.xtools.user.domain.entity.Permission;
import com.cloud.xtools.user.domain.query.admin.PermissionQuery;
import com.cloud.xtools.user.domain.request.admin.PermissionCreateRequest;
import com.cloud.xtools.user.domain.request.admin.PermissionUpdateRequest;
import com.cloud.xtools.user.domain.resource.admin.PermissionTreeResource;
import com.cloud.xtools.user.service.admin.PermissionService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Daixinguo
 */
@RestController
@RequestMapping("api/user/admin")
@RequiredArgsConstructor
@Validated
public class PermissionController extends BaseController {
    private final PermissionService permissionService;

    @GetMapping("permissions")
    public ApiResponse<PageList> list(@ModelAttribute PermissionQuery permissionQuery) {
        permissionService.treeList();
        return success(permissionService.list(permissionQuery));
    }

    @GetMapping("permissions/tree")
    public ApiResponse<List<PermissionTreeResource>> treeList() {
        return success(permissionService.treeList());
    }

    @PostMapping("permissions")
    public ApiResponse<Object> add(@Valid @RequestBody PermissionCreateRequest permissionCreateRequest) {
        return created(permissionService.add(permissionCreateRequest));
    }

    @PutMapping("permissions/{id}")
    public ApiResponse<Object> edit(@EntityExist(entity = Permission.class) @PathVariable Long id, @Valid @RequestBody PermissionUpdateRequest permissionUpdateRequest) {
        permissionService.edit(id, permissionUpdateRequest);
        return success("更新成功");
    }

    @DeleteMapping("permissions/{id}")
    public ApiResponse<Object> remove(@EntityExist(entity = Permission.class) @PathVariable Long id) {
        permissionService.destroy(id);
        return noContent();
    }
}
